﻿using SimpleInjector;
using Sustainalytics.CDDB.DataConnector;
using Sustainalytics.Synchronization.Interface;
using Sustainalytics.ClientDW.Synchronization.SyncReferenceUniv.ReferenceUniverseLogic;
using Sustainalytics.Entities;
using Sustainalytics.MongoDB.Extensions;
using Sustainalytics.SharedEntities.Corporate;
using System;
using System.Collections.Generic;
using System.Configuration.Abstractions;

namespace Sustainalytics.ClientDW.Synchronization.SyncReferenceUniv
{
    public static class SimpleInjectorConfig
    {
        public static Container Register(
            ILogger logger,
            Dictionary<string, string> gaConnectionStrings)
        {
            var container = new Container();
            container.RegisterAppConfig();
            container.RegisterMisc(logger);
            container.RegisterCollections(gaConnectionStrings);

            container.Verify();

            return container;
        }

        private static void RegisterCollections(
            this Container container,
            Dictionary<string, string> gaConnectionStrings)
        {
            container.RegisterClientDWCollections(gaConnectionStrings["ClientDW"]);
        }

        private static void RegisterClientDWCollections(this Container container, string connectionString)
        {
            container.Register(() => MongoFactory.GetCollectionFromDatabase<ReferenceUniverse>(connectionString));
        }

        private static void RegisterAppConfig(this Container container)
        {
            container.RegisterSingleton(() => ConfigurationManager.Instance.AppSettings);
            container.RegisterSingleton(() => ConfigurationManager.Instance.ConnectionStrings);
        }

        private static void RegisterMisc(this Container container, ILogger logger)
        {
            container.Register(() => logger, Lifestyle.Singleton);
            container.RegisterSingleton<BackOfficeAppSettings>();
            container.RegisterSingleton(() => new CorporateDataRepository(container.GetInstance<BackOfficeAppSettings>().ThriftServer,
                                                                            container.GetInstance<BackOfficeAppSettings>().ThriftPort,
                                                                            container.GetInstance<BackOfficeAppSettings>().HbaseTablePrefix,
                                                                            container.GetInstance<BackOfficeAppSettings>().HiveConnectionString));
            container.Register<ReferenceUniverseCddbQuery>(Lifestyle.Singleton);
        }
    }
}
